using System;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Specialized;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Configuration;

/// <summary>
/// This class creates the custom feed url used by the RssUrlParser to fetch the feed.  It creates two urls, a user
/// url, and a feed configuration.  The user url is the one that is given to the user as the link to their custom feed 
/// and is in the form: http://sitename/feed.xml?FeedId=xxxxxx 
/// The feed configuration is stored in our site resource 
/// and contains all the information required to write the feed.
/// </summary>
public class RssUrlBuilder
{
    private RssDataManager _rdm;

    private RssFeedConfig _feedConfig;
    public RssFeedConfig FeedConfig
    {
        get
        {
            if (_feedConfig == null)
                _feedConfig = _rdm.GetFeedConfig(new Guid(FeedId));
            if (_feedConfig == null)
                _feedConfig = new RssFeedConfig();
            return _feedConfig;
        }
        set { _feedConfig = value; }
    }

    private string _feedId;
    public string FeedId
    {
        get
        {
            Guid userGuid = new Guid(CommerceContext.Current.UserID);
            if (_feedId == null)
                _feedId = _rdm.GetFeedId(userGuid);
            if (_feedId == null)
            {
                _rdm.UpdateFeedConfig(userGuid, FeedConfig);
                _feedId = _rdm.GetFeedId(userGuid);
            }
            return _feedId;
        }
        set { _feedId = value; }
    }

    private string _externalFeedUrl;
    public string ExternalFeedUrl
    {
        get
        {
            if (_externalFeedUrl == null)
            {
                RssConfiguration settings = (RssConfiguration)HttpContext.Current.GetSection("rssConfiguration");

                Uri externalUri = new Uri(HttpContext.Current.Request.Url, settings.HandlerUrl + "?" + 
                    settings.FeedGuidFieldName + "=" + _rdm.GetFeedId(new Guid(CommerceContext.Current.UserID)));
                _externalFeedUrl = externalUri.AbsoluteUri;
            }
            return _externalFeedUrl;
        }
        set
        {
            this._externalFeedUrl = value;
        }
    }

    public RssUrlBuilder()
    {
        _rdm = new RssDataManager();
    }

    public CatalogItemsDataSet SearchByKeyword(string query)
    {
        return _rdm.SearchByName(query);
    }

    public virtual void Save()
    {
        // take the current FeedConfig and save it in the RSS Site Resource
        _rdm.UpdateFeedConfig(new Guid(CommerceContext.Current.UserID), FeedConfig);
    }

    // function that is called when user wants to add an item that will always be in their feed
    public virtual void WatchItem(string catalog, string productId)
    {
        RssConfiguration settings = (RssConfiguration)HttpContext.Current.GetSection("rssConfiguration");
        string type = settings.AlwaysInFeedType;
        FeedConfig.AddConfigItem(new RssConfigItem(catalog, productId, type));
    }
    // function that is called when user wants to add an item that will only show up in the feed if it is on sale
    public virtual void WatchForSale(string catalog, string productId)
    {
        RssConfiguration settings = (RssConfiguration)HttpContext.Current.GetSection("rssConfiguration");
        string type = settings.OnSaleType;
        FeedConfig.AddConfigItem(new RssConfigItem(catalog, productId, type));
    }
    // function that is called when user wants to add an item that will only show up in the feed if it is in stock
    public virtual void WatchForInStock(string catalog, string productId)
    {
        RssConfiguration settings = (RssConfiguration)HttpContext.Current.GetSection("rssConfiguration");
        string type = settings.InStockType;
        FeedConfig.AddConfigItem(new RssConfigItem(catalog, productId, type));
    }
    // function that is called when user wants to add the num newest items to their feed.
    public virtual void WatchNewestItems(int howMany)
    {
        FeedConfig.NewItems = howMany;
    }

    public virtual void RemoveItem(string catalog, string productId, string type)
    {
        FeedConfig.RemoveConfigItem(new RssConfigItem(catalog, productId, type));
    }
}
